<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Client;

if (!function_exists('convertsArray')) {

    /**
     *  对象转化数组
     * @param $object
     * @return array
     */
    function convertsArray($object) {
        return collect($object)->map(function ($x) {
                    return (array) $x;
                })->toArray();
    }

}

if (!function_exists('getBday')) {

    /**
     *  计算年龄
     * @param bool $birthday
     * @return false|int|null
     */
    function getBday($birthday = false) {
        if ($birthday === '0000-00-00'){
            return null;
        }
        $age = strtotime($birthday);
        if ($age === false) {
            return null;
        }
        list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
        $now = strtotime("now");
        list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
        $age = $y2 - $y1;
        if ((int) ($m2 . $d2) < (int) ($m1 . $d1))
            $age -= 1;
        return $age;
    }

}

if (!function_exists('getScale')) {
    /**
     *  获取百分比，保留n位小数位, 不加%
     * @param $num
     * @param $count
     * @param $w
     * @return string
     */
    function getScale($num, $count, $w = 2)
    {
        if (!$count) {
            return 0;
        }
        return sprintf('%.2f', $num * 100 / $count);
    }
}

function DBSelect($sql, $params = [], $default = []) {
    $data = DB::select($sql, $params);
    $arrData = collect($data)->map(function ($x) {
                return (array) $x;
            })->toArray();

    if (empty($arrData)) {
        return $default;
    }
    return $arrData;
}

function DBSelectOne($sql, $params = [], $default = []) {
    $data = DB::select($sql, $params);
    $arrData = collect($data)->map(function ($x) {
                return (array) $x;
            })->toArray();

    if (empty($arrData)) {
        return $default;
    }
    return $arrData[0];
}

function groupBy($array, $column) {
    return collect($array)->groupBy($column)->toArray();
}

if (!function_exists('requestCommonApiUrl')) {

    /**
     * 调取common-api接口
     * @param $params
     * @param string $uri
     * @param string $way
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    function requestCommonApiUrl($params, $uri = '', $way = 'POST') {
        try {
            $client = new Client();
            $result = $client->request($way, env('COMMON_API_HOST') . $uri, $params);

            if ($result->getStatusCode() === 200) {
                $body = json_decode($result->getBody(), true);
                if (intval($body['status']) == '200') {
                    $body['status'] = 0;
                } else {
                    $body['status'] = -1;
                }
                return $body;
            } else {
                Log::info('请求出错,请稍后重试, 状态码:' . $result->getStatusCode());
                return response([
                    'status' => -1,
                    'msg' => '请求出错,请稍后重试'
                ]);
            }
        } catch (\Exception $e) {
            Log::error("请求错误: " . $e->getMessage() . ' 参数:', $params);
            return response([
                'status' => -1,
                'msg' => '请求出错,请稍后重试'
            ]);
        }
    }

}